/**
 * respons js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var mainpage =require('../db/mainpage');

function processMainPage(request,response,callback){

	var debug =0;
	if(typeof request.debug === 'undefined' || request.debug ===null ){
		debug =0;
		
	}else{
		debug = request.debug;
	}
	
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	

	var pageType=0;
	var param=null;
	try {
	    param = JSON.parse(request.data);
         
	} catch (e) {
	}
	if(param && param.pageType){
		pageType=param.pageType;
	}
		
	mainpage.getMainPage(request.token, debug,pageType, function(err,obj){
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
		}else{
			response.errorCode=consts.Error.NONE_ERROR;
			response.message=consts.Message.OK_MESSAGE;
			var string = JSON.stringify(obj);//temp.stringify();
		   response.data= string;
		}
		if(callback){
			callback(response);
		}
	});
	
}

function processMainPageBuyer(request,response,callback){

	var debug =0;
	if(typeof request.debug === 'undefined' || request.debug ===null ){
		debug =0;
		
	}else{
		debug = request.debug;
	}
	
	
	if(!request.muuid){
		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;
		if(callback){
			callback(response);
		}
		return;
	}
		
	mainpage.getMainPageBuyer(request.muuid, debug,0, function(err,obj){
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
		}else{
			response.errorCode=consts.Error.NONE_ERROR;
			response.message=consts.Message.OK_MESSAGE;
			var string = JSON.stringify(obj);//temp.stringify();
		   response.data= string;
		}
		if(callback){
			callback(response);
		}
	});
	
}


//获取首页信息
module.exports={
	processMainPage:processMainPage,
	processMainPageBuyer:processMainPageBuyer,
	
};
